package com.sp.scmdb.jobImpl;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;

import com.sp.scmdb.bean.OriginalBean;
import com.sp.scmdb.entity.AlarmType;
import com.sp.scmdb.entity.Area;
import com.sp.scmdb.entity.EntryRecord;
import com.sp.scmdb.entity.LabelIO;
import com.sp.scmdb.entity.LabelReadingMachine;
import com.sp.scmdb.job.DealWithDataJob;
import com.sp.scmdb.service.ParseDataService;
import com.sp.scmdb.utils.ConvertMapToBean;
import com.sp.scmdb.utils.GroupList;

public class DealWithDataJobImpl implements DealWithDataJob {

	private static Log log = LogFactory.getLog(DealWithDataJobImpl.class);

	private ParseDataService parseDataService;

	@Autowired
	public void setParseDataService(ParseDataService parseDataService) {
		this.parseDataService = parseDataService;
	}

	public void doJob() {
		parseOriginal();
	}

	@SuppressWarnings("unchecked")
	public void parseOriginal() {
		try {
			List<AlarmType> alarmTypeList = parseDataService.alarmType();
			Map<Integer, AlarmType> mapAlarmType = GroupList.groupAlarmType(alarmTypeList);
			List<LabelReadingMachine> readerList = parseDataService.findReader();
			if (CollectionUtils.isNotEmpty(readerList)) {
				Map<String, LabelReadingMachine> mapReader = GroupList.groupReaders(readerList);
				String sql = "select top 100 * from OBJ_ORIGINAL order by data_time";
				List orList = parseDataService.findSql(sql);
				if (CollectionUtils.isNotEmpty(orList)) {
					List<Area> listArea = parseDataService.queryRegional();
					orList = ConvertMapToBean.convertMap(orList, OriginalBean.class);
					for (Object object : orList) {
						OriginalBean or = (OriginalBean) object;
						try {
							if (parseDataService.SingleTransaction(or, mapReader, mapAlarmType,
									listArea)) {
								parseDataService.delOriginalBean(or);
							}
						} catch (Exception e) {
							parseDataService.delOriginalBean(or);
							log.error("operator parseOriginal error:" + e.getMessage());
							e.printStackTrace();
						}
					}
				}
			}
		} catch (Exception e) {
			log.error("operator parseOriginal error:" + e.getMessage());
		}
	}
}